/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hendro.latihan.dao.impl;

import com.hendro.latihan.dao.StockDao;
import com.hendro.latihan.entity.Stock;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Hendro
 */
public class StockDaoImpl implements StockDao {

    private Connection connection;

    private final String inserStock = "INSERT INTO `STOCK` (NAMA_BARANG, QUANTITY, HARGA, TANGGAL_PEMESANAN) VALUES(?,?,?,?)";
    
    private final String updateStock = "UPDATE `STOCK` SET NAMA_BARANG = ?, QUANTITY = ?, HARGA = ?, TANGGAL_PEMESANAN = ? WHERE ID = ?";
    
    private final String deleteStock = "DELETE FORM `STOCK` WHERE ID = ?";
    
    private final String getById = "SELECT * FROM `STOCK` WHERE ID = ?";
    
    private final String selectAll = "SELECT * FROM `STOCK`";

    public StockDaoImpl(Connection connection) {
        this.connection = connection;
    }

    @Override
    public void insertStock(Stock stock) throws SQLException {
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(inserStock);
            statement.setString(1, stock.getNamaBarang());
            statement.setInt(2, stock.getQuantity());
            statement.setInt(3, stock.getHarga());
            statement.setDate(4, new Date(stock.getTanggalPemesanan().getTime()));
            statement.executeUpdate();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        } finally {
            if(statement != null){
                statement.close();
            }
        }
    }

    @Override
    public void updateStock(Stock stock) throws SQLException {
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(updateStock);
            statement.setString(1, stock.getNamaBarang());
            statement.setInt(2, stock.getQuantity());
            statement.setInt(3, stock.getHarga());
            statement.setDate(4, new Date(stock.getTanggalPemesanan().getTime()));
            statement.setInt(5, stock.getId());
            statement.executeUpdate();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        } finally {
            if(statement != null){
                statement.close();
            }
        }
    }

    @Override
    public void deleteStock(Integer id) throws SQLException {
        PreparedStatement statement = null;
        try{
            statement = connection.prepareCall(deleteStock);
            statement.setInt(1, id);
            statement.executeUpdate();
        }catch(SQLException ex){
             System.out.println(ex.getMessage());
        }finally{
            if(statement != null){
                statement.close();
            }
        }
    }

    @Override
    public Stock getById(Integer id) throws SQLException {
        PreparedStatement statement = null;
        try{
            statement = connection.prepareStatement(getById);
            statement.setInt(1, id);
            
            ResultSet resultSet = statement.executeQuery();
            Stock stock = null;
            if(resultSet.next()){
                stock.setHarga(resultSet.getInt("ID"));
                stock.setNamaBarang(resultSet.getString("NAMA_BARANG"));
                stock.setQuantity(resultSet.getInt("QUANTITY"));
                stock.setHarga(resultSet.getInt("HARGA"));
                stock.setTanggalPemesanan(resultSet.getDate("TANGGAL_PEMESANAN"));
            }else {
                throw new SQLException("Stock dengan ID tersebut tidak ada");
            }
            return stock;
        } catch (SQLException ex){
            throw new SQLException(ex.getMessage());
        } finally{
            if(statement != null){
                statement.close();
            }
        }
    }

    @Override
    public List<Stock> selectAll() throws SQLException {
        Statement statement = null;
        try {
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(selectAll);
            
            List<Stock> list = new ArrayList<>();
            while (resultSet.next()) {                
                Stock stock = new Stock();
                stock.setId(resultSet.getInt("ID"));
                stock.setNamaBarang(resultSet.getString("NAMA_BARANG"));
                stock.setQuantity(resultSet.getInt("QUANTITY"));
                stock.setHarga(resultSet.getInt("HARGA"));
                stock.setTanggalPemesanan(resultSet.getDate("TANGGAL_PEMESANAN"));
                list.add(stock);
            }
            return list;
        } catch (SQLException ex){
            throw new SQLException(ex.getMessage());
        } finally{
            if(statement != null){
                statement.close();
            }
        }
    }

}
